package com.sunleader.netboot.module.machine.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sunleader.netboot.module.common.dto.Result;
import com.sunleader.netboot.module.machine.dto.ExistDto;
import com.sunleader.netboot.module.machine.dto.Lease;
import com.sunleader.netboot.module.machine.dto.MachineDto;
import com.sunleader.netboot.module.machine.dto.NeedReloadDto;
import com.sunleader.netboot.module.machine.model.Machine;
import com.sunleader.netboot.module.machine.model.Rom;
import com.sunleader.netboot.module.machine.service.MachineService;
import com.sunleader.netboot.module.machine.service.RomService;
import com.sunleader.netboot.module.machine.util.SSHTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.sqlite.util.StringUtils;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/machine")
public class MachineController {
    @Autowired
    private MachineService machineService;
    @Autowired
    private RomService romService;

    @GetMapping("/list")
    private Result<List<Machine>> getMachines(){
        List<Machine> machines = machineService.list();
        return Result.success(machines);
    }
    @GetMapping("/enable_machine/{mac}")
    private Result<Machine> getMachines(@PathVariable(value="mac") String mac){
        Machine machine = machineService.getById(mac);
        if(Objects.isNull(machine)) return Result.success(Machine.builder().needReload(false).build());
        return new Result<>(machine);
    }
    @PostMapping("/enable_machine/{mac}")
    private Result<NeedReloadDto> enableMachine(@PathVariable(value="mac") String mac){
        UpdateWrapper<Machine> wrapper = new UpdateWrapper<>();
        wrapper.eq("mac",mac).set("need_reload",true).set("status","pxe");
        boolean updated = machineService.update(wrapper);
        return updated?Result.success(new NeedReloadDto(true)):Result.failed("Enable failed");
    }
    @PostMapping("/disable_machine/{mac}")
    private Result<NeedReloadDto> disableMachine(@PathVariable(value="mac") String mac){
        UpdateWrapper<Machine> wrapper = new UpdateWrapper<>();
        wrapper.eq("mac",mac).set("need_reload",false).set("status","dhcp");
        boolean updated = machineService.update(wrapper);
        return updated?Result.success(new NeedReloadDto(false)):Result.failed("Enable failed");
    }
    @GetMapping("/mac/{mac}")
    private Result<Machine> findByMac(@PathVariable(value="mac") String mac){
        Machine machine = machineService.getById(mac);
        if(Objects.isNull(machine)) return Result.failed("无此设备");
        return new Result<>(machine);
    }
    @PostMapping("/ip")
    private Result<Boolean> ip(@RequestBody MachineDto machineDto){
        UpdateWrapper<Machine> wrapper = new UpdateWrapper<>();
        wrapper.eq("mac",machineDto.getMac()).set("ip",machineDto.getIp());
        machineService.update(wrapper);
        return new Result<>(Boolean.TRUE);
    }
    @PostMapping("/rom")
    private Result<Boolean> rom(@RequestBody MachineDto machineDto){
        UpdateWrapper<Machine> wrapper = new UpdateWrapper<>();
        wrapper.eq("mac",machineDto.getMac()).set("rom",machineDto.getRom());
        machineService.update(wrapper);
        return new Result<>(Boolean.TRUE);
    }
    @PutMapping("/mac")
    private Result<?> updateByMac(@RequestBody MachineDto machineDto){
        boolean updated = machineService.updateById(machineDto.parseToDo());
        return updated?Result.success():Result.failed("Update failed");
    }
    @DeleteMapping("/mac/{mac}")
    private Result<?> deleteByMac(@PathVariable(value="mac") String mac){
        boolean deleted = machineService.removeById(mac);
        return deleted?Result.success():Result.failed("Delete failed");
    }
    @PostMapping("/add_machine")
    private Result<Machine> addMachine(@RequestBody MachineDto machineDto){
        Machine machine = machineDto.parseToDo();
        machineService.save(machine);
        return new Result<>(machine);
    }
    @GetMapping("/is_white/{mac}")
    private Result<ExistDto> isWhite(@PathVariable(value="mac") String mac){
        Machine machine = machineService.getById(mac);
        ExistDto existDto = new ExistDto(Objects.nonNull(machine));
        return Result.success(existDto);
    }
    @GetMapping("/lease")
    private Result<Lease> lease(){
        List<Machine> machines = machineService.list();
        Lease lease = new Lease(machines);
        return Result.success(lease);
    }
    @PostMapping("/status")
    private Result<Machine> statusUpdate(@RequestBody MachineDto machineDto){
        UpdateWrapper<Machine> wrapper = new UpdateWrapper<>();
        wrapper.eq("mac",machineDto.getMac()).set("status",machineDto.getStatus());
        machineService.update(wrapper);
        return Result.success();
    }
    @PostMapping("/reboot/{mac}/{pwd}")
    private Result<Machine> statusUpdate(@PathVariable(value="mac") String mac,@PathVariable(value="pwd") String pwd){
        Machine machine = machineService.getById(mac);
        try {
            SSHTool.reboot(machine.getIp(),pwd);
        }catch (Exception e){
            return Result.failed("reboot failed");
        }
        return Result.success();
    }
    @GetMapping("/rom/list")
    private Result<List<Rom>> romList(){
        List<Rom> roms = romService.list();
        return Result.success(roms);
    }
    @PostMapping("/rom/add")
    private Result<Rom> romList(@RequestBody Rom rom){
        if("".equals(rom.getName())||"".equals(rom.getPath())){
            return Result.failed("not empty");
        }
        romService.save(rom);
        return Result.success(rom);
    }
    @DeleteMapping("/rom/{name}")
    private Result<Boolean> romList(@PathVariable(value="name") String name){
        boolean del = romService.removeById(name);
        return Result.success(del);
    }
}
